package com.woniuxy.predicates;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @Author: 马宇航
 * @Do: 自定义断言工厂，mygirl 自定义断言工厂，只允许参数年龄在 配置的范围内生效
 * @DateTime: 22/06/27/0027 下午 3:29
 * @Component: 成都蜗牛学苑
 **/
/**
*<img src = "https://woniumd.oss-cn-hangzhou.aliyuncs.com/java/mayuhang/202206271531845.png">
 * @author 马宇航
**/
@Component
public class MyGirlsRoutePredicateFactory extends AbstractRoutePredicateFactory<MyGirlsRoutePredicateFactory.Config> {
    public MyGirlsRoutePredicateFactory() {
        super(MyGirlsRoutePredicateFactory.Config.class);
    }


    /**
     * 具体的自定义断言方法
     * <img src = "https://woniumd.oss-cn-hangzhou.aliyuncs.com/java/mayuhang/202206271531845.png">
     * ChangeLog : 1. 创建 (22/06/27/0027 下午 4:24 [马宇航]);
     * @param config  就是我们的内部类封装的config，包含配置文件中的数据
     * @return java.util.function.Predicate<org.springframework.web.server.ServerWebExchange>
    */
    @Override
    public Predicate<ServerWebExchange> apply(Config config) {

        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                //获取httpRequest
                ServerHttpRequest request = serverWebExchange.getRequest();
                //获取请求中带的参数
                MultiValueMap<String, String> queryParams = request.getQueryParams();
                //取出我需要的目标值
                String myGirls = queryParams.get("MyGirls").get(0);
                int myGirlAge = Integer.parseInt(myGirls);
                //我们配置的config - MyGirls=18,60 前后顺序，shortcutFieldOrder()来决定值的顺序
                int minAge = config.getMinAge();
                int maxAge = config.getMaxAge();
                if (myGirlAge>minAge && myGirlAge<maxAge){
                    return true;
                }
                return false;
            }
        };
    }
    /**
     * 判断配置yml中的顺序和我们config的属性对应顺序。
     * ChangeLog : 1. 创建 (22/06/27/0027 下午 4:32 [马宇航]);
     *
     * @return java.util.List<java.lang.String>
    */
    @Override
    public List<String> shortcutFieldOrder() {
        //自动封装到Config中！
        return Arrays.asList("minAge","maxAge");
    }
    /**
    * @Author: 马宇航
    * @Do: 约定写法！Config！
     * 最后会在public Predicate<ServerWebExchange> apply(Config config) 方法中，注入进去。
    * @DateTime:  22/06/27/0027 下午 4:33
    * @Component: 成都蜗牛学苑
    **/
    static class Config{
        private int minAge;
        private int maxAge;

        public Config() {
        }

        public int getMinAge() {
            return minAge;
        }

        public void setMinAge(int minAge) {
            this.minAge = minAge;
        }

        public int getMaxAge() {
            return maxAge;
        }

        public void setMaxAge(int maxAge) {
            this.maxAge = maxAge;
        }
    }
}
